package com.person.jiaiplan.util;

import cn.hutool.core.util.StrUtil;
import com.person.jiaiplan.contant.CommonConstants;
import com.person.jiaiplan.pojo.entity.ScheduleLogs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
public class UploadUtils {

    /**
     * 获取最终文件上传的路径
     *
     * @param scheduleLogs
     * @param clockDate
     * @return
     */
    public static String getRootPath(ScheduleLogs scheduleLogs, String clockDate) {
        String mkdir = "";

        if (scheduleLogs == null) {
            mkdir = CommonConstants.DEFAULT_DIR;
            if (StrUtil.isNotBlank(clockDate)) {
                mkdir += File.separator + clockDate;
            }
        } else {
            mkdir += scheduleLogs.getModel();
            if (StrUtil.isNotBlank(scheduleLogs.getContent())) {
                mkdir += File.separator + scheduleLogs.getContent();
            }
            if (StrUtil.isNotBlank(scheduleLogs.getContentDetail())) {
                mkdir += File.separator + scheduleLogs.getContentDetail();
            }
        }
        return CommonConstants.FILE_PATH + mkdir + File.separator;
    }


    public static void uploadFiles(String rootPath, MultipartFile file) {
        File file1 = new File(rootPath);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        log.info("上传文件1:" + file1);
        String originalFilename = file.getOriginalFilename();

        File dest = new File(rootPath + originalFilename);
        log.info("上传文件2:" + dest);
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            log.error("上传文件错误", e);
        }
    }
}
